package com.zc.hadoop.lzmh.log;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.text.DecimalFormat;

import org.apache.hadoop.io.WritableComparable;

/**
 * 调用日志信息统计Writable
 *
 */
public class LogTotalWritable implements WritableComparable<LogTotalWritable> {
	//	平均  50% 90% 99%  最大、最小

	// 要统计的key
	private String logKey;
	// 总值
	private int sum;
	// 数量
	private int count;
	// 平均值
	private double aveValue;
	// 50%平均值
	private double halfAveValue;
	// 90%平均值
	private double ninetyAveValue;
	// 99%平均值
	private double ninetyNineAveValue;
	// 最大值
	private int maxValue;
	// 最小值
	private int minValue;

	// 只要数据在网络中传输，就要用到序列化和反序列化

	// 先数据序列化，把对象（字段）写到字节输出流中
	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(logKey);
		out.writeInt(sum);
		out.writeInt(count);
		out.writeDouble(aveValue);
		out.writeDouble(halfAveValue);
		out.writeDouble(ninetyAveValue);
		out.writeDouble(ninetyNineAveValue);
		out.writeInt(maxValue);
		out.writeInt(minValue);
	}

	// 数据反序列话,把对象从字节输出流中读取出来，并且序列化和反序列化的字段顺序要相同
	@Override
	public void readFields(DataInput in) throws IOException {
		this.logKey = in.readUTF();
		this.sum = in.readInt();
		this.count = in.readInt();
		this.aveValue = in.readDouble();
		this.halfAveValue = in.readDouble();
		this.ninetyAveValue = in.readDouble();
		this.ninetyNineAveValue = in.readDouble();
		this.maxValue = in.readInt();
		this.minValue = in.readInt();

	}

	/**
	 * 比较
	 */
	@Override
	public int compareTo(LogTotalWritable o) {
		int thisValue = this.count;
	    int thatValue = o.count;
	    return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
	}

	DecimalFormat df = new DecimalFormat("######0.00");
	
	@Override
	public String toString() {
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("logKey=" + String.format("%1$-30s", logKey));
		buffer.append(",sum=" + String.format("%1$-10s", sum));
		buffer.append(",count=" + String.format("%1$-10s", count));
		buffer.append(",aveValue=" + String.format("%1$-10s", df.format(aveValue)));
		buffer.append(",halfAveValue=" + String.format("%1$-10s", df.format(halfAveValue)));
		buffer.append(",ninetyAveValue=" + String.format("%1$-10s", df.format(ninetyAveValue)));
		buffer.append(",ninetyNineAveValue=" + String.format("%1$-10s", df.format(ninetyNineAveValue)));
		buffer.append(",maxValue=" + String.format("%1$-10s", maxValue));
		buffer.append(",minValue=" + minValue);
		
		return buffer.toString();
	}

	// 构造函数（空参）
	public LogTotalWritable() {
		super();
	}
	
	// 构造函数
	public LogTotalWritable(String logKey, int count) {
		super();
		this.logKey = logKey;
		this.count = count;
	}

	// get,set

	public String getLogKey() {
		return logKey;
	}

	public void setLogKey(String logKey) {
		this.logKey = logKey;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public double getAveValue() {
		return aveValue;
	}

	public void setAveValue(double aveValue) {
		this.aveValue = aveValue;
	}

	public double getHalfAveValue() {
		return halfAveValue;
	}

	public void setHalfAveValue(double halfAveValue) {
		this.halfAveValue = halfAveValue;
	}

	public double getNinetyAveValue() {
		return ninetyAveValue;
	}

	public void setNinetyAveValue(double ninetyAveValue) {
		this.ninetyAveValue = ninetyAveValue;
	}

	public double getNinetyNineAveValue() {
		return ninetyNineAveValue;
	}

	public void setNinetyNineAveValue(double ninetyNineAveValue) {
		this.ninetyNineAveValue = ninetyNineAveValue;
	}

	public void setNinetyNineAveValue(int ninetyNineAveValue) {
		this.ninetyNineAveValue = ninetyNineAveValue;
	}

	public int getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(int maxValue) {
		this.maxValue = maxValue;
	}

	public int getMinValue() {
		return minValue;
	}

	public void setMinValue(int minValue) {
		this.minValue = minValue;
	}

	public int getSum() {
		return sum;
	}

	public void setSum(int sum) {
		this.sum = sum;
	}
	
}
